package com.basic.modules.nykf.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.ParamsModel;
import com.basic.common.utils.R;
import com.basic.modules.nykf.entity.CustomerComplaintHandlingEntity;
import com.basic.modules.nykf.entity.CustomerEntity;
import com.basic.modules.nykf.entity.CustomerShopEntity;
import com.basic.modules.nykf.service.CustomerComplaintHandlingService;
import com.basic.modules.nykf.service.CustomerService;
import com.basic.modules.sys.entity.SysUserEntity;
import com.basic.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 客诉处理明细
 */

@RestController
@RequestMapping("/new/ykf/customer/complaintDetail")
public class CustomerComplaintHandlingController {

    @Autowired
    private CustomerComplaintHandlingService customerComplaintHandlingService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private CustomerService customerService;

    /**
     * 分页查询投诉明细
     *
     * @param params
     * @return
     */
    @PostMapping("/getComplaintSuggestionDetailList")
    public R getCustomerComplaintHandlingList(@RequestBody ParamsModel<CustomerComplaintHandlingEntity> params) {
        PageUtils page = customerComplaintHandlingService.getCustomerComplaintSuggestionDetailList(params);
        return R.ok(page);
    }

    /**
     * 新增投诉明细
     *
     * @param customerComplaintHandlingEntity
     * @return
     */
    @PostMapping("/addComplaintSuggestionDetail")
    public R addCustomerComplaintSuggestionDetail(@RequestBody CustomerComplaintHandlingEntity customerComplaintHandlingEntity) {

        R result = customerComplaintHandlingService.saved(customerComplaintHandlingEntity);
        return R.ok(result);

    }

    /**
     * 处理投诉明细
     *
     * @param customerComplaintHandlingEntity
     * @return
     */
    @PostMapping("/editComplaintSuggestionDetail")
    public R editCustomerComplaintSuggestionDetail(@RequestBody CustomerComplaintHandlingEntity customerComplaintHandlingEntity) {
        //设置判定时间
        if(StrUtil.isNotEmpty(customerComplaintHandlingEntity.getTsjgpdVal())){
            customerComplaintHandlingEntity.setPdsj(new Date());
        }else{
            customerComplaintHandlingEntity.setPdsj(null);
        }
        //设置完结时间
        if("已完结".equals(customerComplaintHandlingEntity.getWjztVal())){
            customerComplaintHandlingEntity.setWjsj(new Date());
        }else{
            customerComplaintHandlingEntity.setWjsj(null);
        }
        Long complaintDetailId = customerComplaintHandlingEntity.getComplaintDetailId();
        Long complaintId = customerComplaintHandlingEntity.getComplaintId();

        //更新已有记录
        if (complaintDetailId != null && complaintId != null) {
            UpdateWrapper<CustomerComplaintHandlingEntity> UpdateWrapper = new UpdateWrapper<CustomerComplaintHandlingEntity>()
                    .eq("complaint_detail_id", complaintDetailId)
                    .set("tskhmc",customerComplaintHandlingEntity.getTskhmc())
                    .set("tskhdp",customerComplaintHandlingEntity.getTskhdp())
                    .set("tsjgpd_val",customerComplaintHandlingEntity.getTsjgpdVal())
                    .set("wgwtfby_val",customerComplaintHandlingEntity.getWgwtfbyVal())
                    .set("wgwtfbe_val",customerComplaintHandlingEntity.getWgwtfbeVal())
                    .set("tsclzt_val",customerComplaintHandlingEntity.getTsclztVal())
                    .set("zrrqy_val",customerComplaintHandlingEntity.getZrrqyVal())
                    .set("zrr",customerComplaintHandlingEntity.getZrr())
                    .set("clfs_val",customerComplaintHandlingEntity.getClfsVal())
                    .set("wjzt_val",customerComplaintHandlingEntity.getWjztVal())
                    .set("wjsj", customerComplaintHandlingEntity.getWjsj())
                    .set("pdsj",customerComplaintHandlingEntity.getPdsj())
                    .set("zdclrye",customerComplaintHandlingEntity.getZdclrye());
            customerComplaintHandlingService.update(customerComplaintHandlingEntity, UpdateWrapper);
            return R.ok();
        } else if (complaintDetailId == null && complaintId != null) {
            customerComplaintHandlingEntity.setJqsj(new Date());
            customerComplaintHandlingService.save(customerComplaintHandlingEntity);
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 查询公司对应的客服人员
     *
     * @param map
     * @return
     */
    @PostMapping("/getKeFuList")
    public R getKeFuList(@RequestBody Map map) {
        String companyName = map.get("companyName").toString();
        String postName = map.get("postName").toString();
        List<SysUserEntity> list = sysUserService.getKueFuList(companyName, postName);
        return R.ok(list);
    }

    /**
     * 查询投诉客户名称
     *
     * @return
     */
    @PostMapping("/getCustomerList")
    public R getCustomerNameList() {
        List<CustomerEntity> customerNameList = customerService.getCustomerNameList();
        return R.ok(customerNameList);
    }

    /**
     * 投诉客户店铺
     *
     * @param map
     * @return
     */
    @PostMapping("/getCustomerShopList")
    public R getCustomerShopList(@RequestBody Map map) {
        String tskhmc = map.get("tskhmc").toString();
        List<CustomerShopEntity> customerShopList = customerService.getCustomerShopList(tskhmc);
        return R.ok(customerShopList);
    }

    @PostMapping("/getTsmx")
    public R getTsmx(@RequestBody Map map){
        String djsj = map.get("djsj").toString();
        Map map1 = customerComplaintHandlingService.getByDate(djsj);
        return R.ok(map1);
    }


}
